package com.aoxin.service.impl.course.systemCourse;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.course.systemCourse.SchoolCourseDao;
import com.aoxin.mapper.course.systemCourse.SlfOrderCourseMapper;
import com.aoxin.mapper.course.systemCourse.SlfSyllabusMapper;
import com.aoxin.mapper.course.webCourse.SlfClassMapper;
import com.aoxin.mapper.opm.SlfClassroomMapper;
import com.aoxin.mapper.opm.SlfEmployeeMapper;
import com.aoxin.model.course.SlfClass;
import com.aoxin.model.course.SlfSyllabus;
import com.aoxin.model.dto.CourseDto;
import com.aoxin.model.dto.SelectDto;
import com.aoxin.model.dto.SlfSyllabusDto;
import com.aoxin.model.opm.SlfEmployee;
import com.aoxin.service.inter.course.systemCourse.SyllabusService;
import com.aoxin.util.ErrorUtil;

@Service("syllabusService")
public class SyllabusServiceImpl implements SyllabusService {

	@Autowired
	private SlfSyllabusMapper slfSyllabusMapper;

	@Autowired
	private SlfOrderCourseMapper slfOrderCourseMapper;

	@Autowired
	private SlfEmployeeMapper slfEmployeeMapper;

	@Autowired
	private SlfClassroomMapper SlfClassroomMapper;

	@Autowired
	private SlfClassMapper slfClassMapper;

	@Autowired
	private SchoolCourseDao schoolCourseDao;

	@Override
	@Transactional
	public void insertSyllabus(SlfSyllabus syllabus) {
		Assert.notNull(syllabus, String.format(ErrorUtil.PARAMETER_NULL, "syllabus"));
		checkSyllabus(syllabus);
		syllabus.setCreateDate(new Date());
		this.slfSyllabusMapper.insertSelective(syllabus);
	}

	@Override
	@Transactional
	public void deleteSyllabuss(List<Integer> syllabusIds) {
		Assert.notEmpty(syllabusIds, String.format(ErrorUtil.PARAMETER_NULL, "syllabusIds"));
		Map<String, Object> map = new HashMap<String, Object>();
		for (Integer integer : syllabusIds) {
			map.put("syllabusId", integer);
			if (this.slfOrderCourseMapper.getList(map).size() > 0) {
				throw new IllegalArgumentException(ErrorUtil.HAS_ORDERCOURSE);
			}
		}
		this.slfSyllabusMapper.deleteByPrimaryKeys(syllabusIds);
	}

	@Override
	@Transactional
	public void updateSyllabus(SlfSyllabus syllabus) {
		Assert.notNull(syllabus, String.format(ErrorUtil.PARAMETER_NULL, "syllabus"));
		checkSyllabus(syllabus);
		this.slfSyllabusMapper.updateByPrimaryKeySelective(syllabus);
	}

	@Override
	public SlfSyllabusDto getSyllabus(Integer syllabusId) {
		Assert.notNull(syllabusId, String.format(ErrorUtil.PARAMETER_NULL, "syllabusId"));
		return this.slfSyllabusMapper.selectByPrimaryKey(syllabusId);
	}

	@Override
	public List<SlfSyllabus> getSyllabusList(Map<String, Object> map) {
		return this.slfSyllabusMapper.getList(map);
	}

	@Override
	public List<CourseDto> getSyllabusListByChildren(Map<String, Object> map) {
		Iterator keys = map.keySet().iterator();
		Integer exist = 0;
		while (keys.hasNext()) {
			String key = (String) keys.next();
			if ("startDate".equals(key)) {
				exist = 1;
			}
		}
		if (exist == 0) {
			map.put("startDate", new DateTime().toString("yyyy-MM-dd"));
		}
		return this.slfSyllabusMapper.getListByChildren(map);
	}

	@Override
	public List<SlfSyllabus> disByPrimaryKey(SlfSyllabus slfSyllabus) {
		Assert.notNull(slfSyllabus, String.format(ErrorUtil.EMPLOYEE_Name, "slfSyllabus"));
		List<SlfSyllabus> list = slfSyllabusMapper.disByPrimaryKey(slfSyllabus);
		return list;
	}

	@Override
	public List<SelectDto> getSelectWeek() {
		return this.slfSyllabusMapper.getSelectWeek();
	}

	/**
	 * 检查课表
	 * 
	 * @param syllabus
	 */
	private void checkSyllabus(SlfSyllabus syllabus) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startDate", syllabus.getStartDate());
		map.put("endDate", syllabus.getEndDate());
		map.put("classroomId", syllabus.getClassroomId());
		if (this.slfSyllabusMapper.checkClassroomDate(map) > 0) {
			throw new IllegalArgumentException(String.format(ErrorUtil.CHECK_CLASSROOM_DATE, syllabus.getClassroomName()));
		}
		map.remove("classroomId");
		// 判断同一时间班级是否已安排
		map.put("classId", syllabus.getClassId());
		if (this.slfSyllabusMapper.checkClassroomDate(map) > 0) {
			throw new IllegalArgumentException(String.format(ErrorUtil.CHECK_CLASS_DATE, syllabus.getClassName()));
		}
		map.remove("classroomId");
		// 判断同一时间科任老师是否有课
		map.put("employeeId", syllabus.getEmployeeId());
		if (this.slfSyllabusMapper.checkClassroomDate(map) > 0) {
			throw new IllegalArgumentException(String.format(ErrorUtil.CHECK_EMPLOYEE_DATE, syllabus.getEmployeeName()));
		}
		SlfEmployee employee = this.slfEmployeeMapper.selectByPrimaryKey(syllabus.getEmployeeId());
		syllabus.setEmployeeName(employee.getName());
		SlfClass slfClass = new SlfClass();
		slfClass.setClassId(syllabus.getClassId());
		List<SlfClass> slfClasss = this.slfClassMapper.getClasses(slfClass);
		if (slfClasss.size() > 0) {
			String className = this.slfClassMapper.getClasses(slfClass).get(0).getName();
			syllabus.setClassName(className);
		}
		syllabus.setSchoolId(employee.getSchoolId());
		String classroomName = this.SlfClassroomMapper.selectByPrimaryKey(syllabus.getClassroomId()).getName();
		syllabus.setClassroomName(classroomName);
		String courseName = this.schoolCourseDao.getSchoolCourseById(syllabus.getCourseId()).getName();
		syllabus.setCourseName(courseName);
	}

	@Override
	public List<String> getSyllabusDate(Integer schoolId, String ymdDate) {
		return this.slfSyllabusMapper.getSyllabusDate(schoolId, ymdDate);
	}

	@Override
	public CourseDto getSyllabusByChildren(Map<String, Object> map) {
		return this.slfSyllabusMapper.getSyllabusByChildren(map);
	}
}
